﻿<?xml version="1.0" encoding="utf-8"?>
<project name="MDownloader" default="all">

  <script language="C#" prefix="versioning" verbose="false">
    <imports>
      <import namespace="System.Security.Cryptography"/>
      <import namespace="System.Text"/>
    </imports>
    <code>
      <![CDATA[
              [Function("GetVersionGuid")]
              public static string GetVersionGuid(string application, string version) 
              {
                Guid guid = new Guid("{D0A3535D-6678-455f-A2C3-E978C636724B}");
                byte[] data = Encoding.UTF8.GetBytes(application + version.Substring(0, version.LastIndexOf('.')));
                MD5 encoder = MD5.Create();
                byte[] hash = encoder.ComputeHash(data);
                
                for (int i = 0; i < hash.Length; i++)
                    hash[i] = (byte)((hash[i] + guid.ToByteArray()[i]) % 256);
                    
                return new Guid(hash).ToString();
              }
      ]]>
    </code>
  </script>

  <script language="C#" prefix="codeplex" verbose="false">
    <references>
      <include name="System.dll"/>
      <include name="System.Web.Services.dll"/>
      <include name="tools\codeplex\CodePlex.WebServices.Client.dll"/>
      <include name="tools\codeplex\CodePlex.WebServices.Client.XmlSerializers.dll"/>
    </references>
    <imports>
      <import namespace="System.Collections.Generic"/>
      <import namespace="System.Net"/>
      <import namespace="CodePlex.WebServices.Client"/>
    </imports>
    <code>
      <![CDATA[
              [TaskName("publish-artifacts")]
              public class PublishArtifacts : Task
              {
                private string version;
                private string username;
                private string password;
              
                [TaskAttribute("version", Required=true)]
                public string Version
                {
                  get { return this.version; }
                  set { this.version = value; }
                }
              
                [TaskAttribute("username", Required=true)]
                public string Username
                {
                  get { return this.username; }
                  set { this.username = value; }
                }

                [TaskAttribute("password", Required=true)]
                public string Password
                {
                  get { return this.password; }
                  set { this.password = value; }
                }

                protected override void ExecuteTask()
                {
                  CodePlex.WebServices.Client.Proxy.ReleaseServiceProxy.ReleaseService proxy = new CodePlex.WebServices.Client.Proxy.ReleaseServiceProxy.ReleaseService();
                  ReleaseService service = new ReleaseService(proxy);
                  List<ReleaseFile> releases = new List<ReleaseFile>();

                  proxy.Timeout = 10 * 60 * 1000;

                  releases.Add(new ReleaseFile("ROrganizer-" + this.Version + ".msi", File.ReadAllBytes(@"build\ROrganizer-" + this.Version + ".msi"), ReleaseFileType.RuntimeBinary));
                  releases.Add(new ReleaseFile("ROrganizer-" + this.Version + ".zip", File.ReadAllBytes(@"build\ROrganizer-" + this.Version + ".zip"), ReleaseFileType.RuntimeBinary));
                  releases.Add(new ReleaseFile("ROrganizer-" + this.Version + "-src.zip", File.ReadAllBytes(@"build\ROrganizer-" + this.Version + "-src.zip"), ReleaseFileType.SourceCode));

                  service.Credentials = new NetworkCredential(this.Username, this.Password);
                  service.CreateARelease("rorganizer", "ROrganizer-" + this.Version, "description", null, ReleaseStatus.Planning, false, false);
                  service.UploadReleaseFiles("rorganizer", "ROrganizer-" + this.Version, releases);
                }
              }
      ]]>
    </code>
  </script>

  <target name="prepare">
    <property name="nant.settings.currentframework" value="net-4.0" />
    <property name="version" value="${build.number}"/>
    <property name="defines" value="NUNIT"/>

    <mkdir dir="build" if="${not directory::exists('build')}"/>
  </target>

  <target name="clean" depends="prepare">
    <delete>
      <fileset basedir="build">
        <include name="**.*"/>
      </fileset>
    </delete>
  </target>

  <target name="compile" depends="clean">
    <loadtasks assembly="tools\nant-contrib\bin\NAnt.Contrib.Tasks.dll" />

    <foreach item="File" property="filename">
      <in>
        <items basedir="src">
          <include name="**\AssemblyInfo.cs"/>
        </items>
      </in>
      <do>
        <exec program="tools\update-version\UpdateVersion.exe"
              commandline="-p ${version} -v Assembly -i ${filename} -o ${filename}"
              verbose="true" failonerror="true" />
        <exec program="tools\update-version\UpdateVersion.exe"
              commandline="-p ${version} -v File -i ${filename} -o ${filename}"
              verbose="true" failonerror="true" />
      </do>
    </foreach>

    <msbuild project="src\ROrganizer.sln" verbosity="Minimal" target="Clean">
      <property name="Configuration" value="Release" />
      <property name="OutputPath" value="${path::get-full-path('build')}"/>
      <property name="DefineConstants" value="${defines}" />
    </msbuild>

    <msbuild project="src\ROrganizer.sln" verbosity="Minimal" target="Rebuild">
      <property name="Configuration" value="Release" />
      <property name="OutputPath" value="${path::get-full-path('build')}" />
      <property name="DefineConstants" value="${defines}" />
    </msbuild>

  </target>

  <target name="test" depends="compile">
    <nunit2 verbose="true">
      <formatter type="Xml" usefile="true" extension=".xml" outputdir="build\" />
      <test>
        <assemblies basedir="build">
          <include name="*.Tests.dll"/>
        </assemblies>
      </test>
    </nunit2>
  </target>

  <target name="build_src" depends="compile, test">
    <zip zipfile="build\ROrganizer-${version}-src.zip" ziplevel="9" comment="ROrganizer-${version}">
      <fileset basedir="." prefix="MDownloader-${version}-src">
        <include name="**\*"/>
        <exclude name="build\**\*"/>
        <exclude name="src\**\obj\**\*"/>
        <exclude name="**\*.snk"/>
        <exclude name="**\*.pfx"/>
        <exclude name="**\*.suo"/>
        <exclude name="**\*.sln.cache"/>
        <exclude name="**\*.csproj.user"/>
        <exclude name="**\*.csproj.vspscc"/>
        <exclude name="**\*.vssscc"/>
      </fileset>
    </zip>
  </target>

  <target name="build_zip" depends="compile, test">
    <zip zipfile="build\ROrganizer-${version}.zip" ziplevel="9" comment="ROrganizer-${version}">
      <fileset basedir="build" prefix="ROrganizer-${version}">
        <include name="**\*"/>
        <exclude name="*.Tests.dll"/>
        <exclude name="Nunit.Framework.dll"/>
        <exclude name="*.xml"/>
        <exclude name="Microsoft.Deployment.*"/>
        <exclude name="*.msi"/>
        <exclude name="*.zip"/>
        <exclude name="*.pfx"/>
      </fileset>
    </zip>
  </target>

  <target name="build_msi" depends="compile, test">
    <loadtasks assembly="tools\nant-wix\Microsoft.Tools.WindowsInstallerXml.NAntTasks.dll" />

    <candle out="build\ROrganizer-${version}.wixobj" exedir="tools\wix\bin\" extensions="WixUIExtension">
      <sources>
        <include name="build.wxs"/>
      </sources>
      <defines>
        <define name="version" value="${version}"/>
        <define name="upgrade" value="${versioning::GetVersionGuid('ROrganizer', version)}"/>
      </defines>
    </candle>

    <light out="build\ROrganizer-${version}.msi" exedir="tools\wix\bin\" extensions="WixUIExtension;WixNetFxExtension" rebuild="true">
      <sources>
        <include name="build\ROrganizer-${version}.wixobj"/>
      </sources>
    </light>

    <delete>
      <fileset basedir="build">
        <include name="ROrganizer-${version}.wix*"/>
      </fileset>
    </delete>
  </target>

  <target name="publish_artifacts" depends="build_zip, build_msi, build_src"
          if="${property::exists('codeplex.publish') and property::exists('codeplex.username') and property::exists('codeplex.password')}">
    <publish-artifacts version="${version}"
                       username="${codeplex.username}"
                       password="${codeplex.password}"
                       if="${codeplex.publish == 'true'}"/>
  </target>

  <target name="all" depends="publish_artifacts"/>
  
</project>